<template>
  <!-- 短信接收 -->
  <div id="base">
    <!-- 搜索 -->
    <div class="search">
      <div class="searchTop">
        <el-form :inline="true" :model="search">
          <el-form-item label="用户名">
            <el-input v-model="search.userName" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getAddBook">查询</el-button>
            <el-button @click="empty" type="info">清空</el-button>
            <el-button type="primary" @click="notice">新增</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!-- 列表 -->

    <div id="table">
      <el-table
        :data="
          data.list.slice(
            (data.currpage - 1) * data.pagesize,
            data.currpage * data.pagesize
          )
        "
        border
        style="width: 100%"
        :header-cell-style="{ background: 'rgba(237, 242, 246, 1)' }"
      >
        <!-- <el-table-column type="index" label="序号" width="40px"></el-table-column> -->
        <el-table-column :index="table_index" type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="userName" label="用户名"></el-table-column>
        <el-table-column prop="tel" label="手机号"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        
        <!-- <el-table-column label="是否接收">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 1" class="green">是</span>
            <span v-else class="red">否</span>
          </template>
        </el-table-column>-->
        <!-- <el-table-column prop="messageType" label="预警级别"></el-table-column> -->
        <!-- <el-table-column label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.reciveStatus == 1" class="green">启用</span>
            <span v-else class="red">禁用</span>
          </template>
        </el-table-column>-->
        <el-table-column prop="createName" label="创建人"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="showdetail(scope.row, scope.$index)">编辑</el-button>
            <el-button type="danger" size="small" @click="showrelease(scope.row, scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        :current-page="data.currpage"
        :page-size="data.pagesize"
        :page-sizes="[5, 10, 15, 20]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="data.list.length"
        @prev-click="nextpage"
        @next-click="nextpage"
        @current-change="nextpage"
        @size-change="sizeChange"
      ></el-pagination>
    </div>

    <!-- 新增/编辑 -->
    <el-dialog
      :title="addedittitle"
      width="450px"
      :visible.sync="showedit"
      @close="resetForm('ruleForm')"
    >
      <el-form
        ref="ruleForm"
        :model="msg"
        :rules="rules"
        label-width="auto"
        class="msg"
        hide-required-asterisk
      >
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="msg.userName" maxlength="20" placeholder="短信接收人姓名"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="tel">
          <el-input v-model="msg.tel" maxlength="11" placeholder="电话"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="msg.email" maxlength="20" placeholder="邮箱"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取消</el-button>
        <el-button v-if="addedittitle == '短信接收人-新增'" type="primary" @click="submitForm">保存</el-button>
        <el-button v-else type="primary" @click="editForm">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name:'Receive',
  data() {
    return {
      addedittitle: null,
      showedit: false,
      editDis: false,
      options: ["闸门名称"],
      types: ["节制闸", "橡胶坝"],
      isConnecteds: ["连接", "断开"],
      msg: {
        name: "",
        tel: "",
        email: "",
        userName: ""
      },
      statusOptions: [
        {
          value: 1,
          label: "是"
        },
        {
          value: 0,
          label: "否"
        }
      ],
      reciveStatusOptions: [
        {
          value: 1,
          label: "启用"
        },
        {
          value: 0,
          label: "禁用"
        }
      ],
      // 新建通知非空验证
      rules: {
        name: [
          { required: true, message: "请输入短信接收人姓名", trigger: "blur" }
        ],
        tel: [
          {
            required: true,
            message: "请输入短信接收人姓名电话",
            trigger: "blur"
          },
          {
            pattern: 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/,
            required: true,
            message: "请填写正确的手机号",
            trigger: "blur"
          }
        ],
        email: [
          {
            required: true,
            message: "请输入短信接收人姓名邮箱",
            trigger: "blur"
          },
          {
            pattern: /^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*\.[a-z]{2,}$/, //通用邮箱正则表达式
            required: true,
            message: "请填写正确的邮箱地址",
            trigger: "blur"
          }
        ],
        createName: [
          { required: true, message: "请输入创建人", trigger: "blur" }
        ]
      },
      dialogFormVisible: false,
      // 搜索
      search: {
        userName: ""
      },
      // 列表数据
      data: {
        pagesize: 10,
        currpage: 1,
        list: []
      }
    };
  },
  created() {
    this.getList();
  },
  computed: {},
  methods: {
    table_index(index) {
      return (this.data.currpage - 1) * this.data.pagesize + (index + 1);
    },
    //获取列表数据
    getReciveStatus(type) {
      switch (type) {
        case 1:
          return "启用";
          break;
        case 0:
          return "禁用";
          break;
      }
    },
    getList() {
      let that = this;
      that.$http
        .post("messageReceiver/findList", {
          userName: that.search.userName
        })
        .then(res => {
          that.data.currpage = 1;
          debugger;
          that.data.list = res.data;
        });
    },
    //取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.showedit = false;
      this.msg = {};
    },
    //提交
    submitForm() {
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          // console.log("新增");
          // let status = this.msg.status === "是" ? 1 : 0;
          // let reciveStatus = this.msg.reciveStatus === "启用" ? 1 : 0;
          // console.log(this.msg.status, this.msg.reciveStatus);
          this.$http
            .post("messageReceiver/add", {
              ...this.msg,
              // status,
              // reciveStatus,
              companyId: localStorage.getItem("companyId")
            })
            .then(res => {
              if (res.code == 1) {
                this.showedit = false;
                this.getList();
                this.$message({
                  type: "success",
                  showClose: true,
                  message: "新增成功",
                  offset: 155
                });
              } else {
                this.$message({
                  type: "warning",
                  showClose: true,
                  message: "新增失败",
                  offset: 155
                });
              }
            });
        } else {
          return false;
        }
      });
    },
    editForm() {
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          // this.msg.status = this.msg.status === "是" ? 1 : 0;
          // this.msg.reciveStatus = this.msg.reciveStatus === "启用" ? 1 : 0;

          let { id, userName, tel, email } = this.msg;
          this.$http
            .post("messageReceiver/update", {
              id,
              userName,
              tel,
              email
              // reciveStatus: this.msg.reciveStatus
            })
            .then(res => {
              if (res.code == 1) {
                this.showedit = false;
                this.getList();
                this.$message({
                  type: "success",
                  showClose: true,
                  message: "编辑成功",
                  offset: 155
                });
              } else {
                this.$message({
                  type: "warning",
                  showClose: true,
                  message: "编辑失败",
                  offset: 155
                });
              }
            });
        } else {
          return false;
        }
      });
    },
    //新增
    notice() {
      this.showedit = true;
      this.editDis = true;
      this.addedittitle = "短信接收人-新增";
    },
    //编辑
    showdetail(row, index) {
      this.showedit = true;
      this.editDis = true;
      this.addedittitle = "短信接收人-编辑";
      this.msg = JSON.parse(JSON.stringify(row));
      // this.msg.status = row.status == 1 ? "是" : "否";
      // this.msg.reciveStatus = row.reciveStatus == 1 ? "启用" : "禁用";

      // console.log({
      //   ...this.msg,
      //   status: row.status,
      //   reciveStatus: row.reciveStatus
      // });
    },
    //删除
    showrelease(row, index) {
      this.$confirm("此操作将删除此数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http
            .post("messageReceiver/deleteById", { id: row.id })
            .then(res => {
              // console.log(res);
              this.getList();
              this.$message({
                type: "success",
                message: res.message,
                offset: 155
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //查询
    getAddBook() {
      this.getList();
    },
    //清空
    empty() {
      (this.search.userName = ""), this.getList();
    },
    // 翻页
    nextpage(value) {
      this.data.currpage = value;
    },
    // 显示条数切换
    sizeChange(total) {
      this.data.pagesize = total;
    }
    // 导出
    // excellist() {}
  }
};
</script>

<style lang="less" scoped>
.search {
  // overflow: hidden;
  display: flex;
  justify-content: space-between;
}
.table {
  width: 1128px;
  height: 465px;
  margin-top: 16px;
}
.table {
  width: 100%;
}

.tableButton3 {
  color: #fff;
  background-color: #f56c6c;
  border-radius: 3px;
}
.textmsg {
  color: red;
}
.textcc {
  color: #000;
}
.paginationList {
  text-align: center;
  margin-top: 32px;
  padding: 0;
}
.table {
  width: 1128px;
  height: 465px;
  margin-top: 16px;
}
.dialogText {
  .el-input {
    width: 240px;
    height: 32px;
  }
  .selectTop {
    width: 240px;
    height: 32px;
  }
  .selectBot {
    width: 240px;
    height: 32px;
  }
}
.formButon {
  width: 127px;
  height: 40px;
  text-align: center;
}

.pagination {
  text-align: center;
  margin-right: 25px;
  padding-top: 20px;
}
.green {
  color: green;
}
.red {
  color: red;
}
</style>
